package 数组.奇偶分离;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/15 17:05
 * @description:剑指offer
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int[] array = {1,3,5};
        int[] ans = exchange(array);
    }

    //采用快速排序的策略
    public static int[] exchange(int[] nums) {
        int right = nums.length - 1;
        int left = 0;
        while (left < right){
            //从左向右扫描，找到第一个偶数
            while (left <= right && nums[left] % 2 != 0){
                left++;
            }
            //从右向左扫描,找到第一个奇数
            while (left <= right && nums[right] % 2 == 0){
                right--;
            }
            if (left <= right && right >= 0){
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
                left++;
                right--;
            }
        }
        return nums;
    }
}
